package com.haisen.service.impl;

import com.alibaba.druid.sql.visitor.functions.Isnull;
import com.haisen.dataobject.entity.Area;
import com.haisen.dataobject.entity.ItemCat;
import com.haisen.dataobject.mapper.base.AreaMapper;
import com.haisen.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

@Service
public class AreaServiceImpl implements AreaService{

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public void create(Area area) {
        //默认添加到都是叶子，1
         Area parent = areaMapper.selectByPrimaryKey(area.getParentId());
         if (parent !=null && parent.getLeaf() == true){
              parent.setLeaf(false);
             areaMapper.updateByPrimaryKeySelective(parent);
         }
        areaMapper.insertSelective(area);
    }

    @Override
    public void update(Area area) {
      areaMapper.updateByPrimaryKeySelective(area);
    }

    @Override
    public void delete(Long areaId) {
        //1、如果有叶子存在，这不能删除，已经在视图中的按钮做了屏蔽了
        //2、如果删除此节点后没有其他叶子，上级节点由树变为叶子
        Area self = areaMapper.selectByPrimaryKey(areaId);

        Weekend<Area> example = Weekend.of(Area.class);
        WeekendCriteria<Area,Object> criteria = example.weekendCriteria();
        criteria.andEqualTo(Area :: getId,self.getParentId());
        int childerCount = areaMapper.selectCountByExample(example);
        if(childerCount < 2 ){
            Area parent = areaMapper.selectByPrimaryKey(self.getParentId());
            parent.setLeaf(true);
            areaMapper.updateByPrimaryKeySelective(parent);
        }

        areaMapper.deleteByPrimaryKey(areaId
        );
    }

    @Override
    public Area findOneById(Long areaId) {
        return areaMapper.selectByPrimaryKey(areaId);
    }

    @Override
    public List<Area> find(Weekend example) {
        return areaMapper.selectByExample(example);
    }

    @Override
    public List<Area> findAll() {
        return areaMapper.selectAll();
    }

    @Override
    public List<Area> getCityCountry(Long pid) {
          Weekend<Area> example = Weekend.of(Area.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",pid);
        return areaMapper.selectByExample(example);
    }

    @Override
    public List<Area> getAllCity() {
        Weekend<Area> example = Weekend.of(Area.class);
        WeekendCriteria<Area,Object> criteria = example.weekendCriteria();
        criteria.andEqualTo(Area :: getLeaf,false);
        return areaMapper.selectByExample(example);
    }

    @Override
    public List<Area> getCityCountry(String name) {
        Weekend<Area> example = Weekend.of(Area.class);
        WeekendCriteria<Area,Object> criteria = example.weekendCriteria();
        criteria.andLike(Area :: getName,'%'+name+'%');
        return areaMapper.selectByExample(example);
    }
}
